package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TBAgentInfoMapper;
import cc.rengu.igas.momp.common.entity.TBAgentInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 机构管理表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/6/22 19:56
 */

public class TBAgentInfoMapperImpl implements TBAgentInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    /**
     * 使用代理商编号查询代理商信息数据库操作实现
     *
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/7/20
     */
    @Override
    public TBAgentInfo selectAgentInfoByPrimaryKey(String instId, String agentCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectAgentInfoByPrimaryKey");
        TBAgentInfo TBAgentInfo = new TBAgentInfo();
        TBAgentInfo.setInstId(instId);
        TBAgentInfo.setAgentCode(agentCode);
        TBAgentInfo.setAgentStatus("01");//01：正式
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, TBAgentInfo, TBAgentInfo);
        if (0 != iReturnCode) {
            rglog.error("获取代理商信息失败,instId:<{}>,agentCode:<{}>,iReturnCode:<{}>", instId, agentCode, iReturnCode);
            return null;
        }
        return TBAgentInfo;
    }
}
